// pages/loadData/loadData.js
Page({
  data: {
    c1: true,
    c2: false,
    bc1: "azure",
    bc2: "rgba(255,255,255,0.6)"
  },


  onLoad(options) {
    this.setData({
      c1: true,
      c2: false
    })
  },
  uploadEmojis(event) {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      maxDuration: 30,
      success(res) {
        // console.log(event.detail.value.eName)
        // console.log(res.tempFiles[0].tempFilePath)
        var filepath = res.tempFiles[0].tempFilePath
        var cloudpath = "emojis/" + event.detail.value.eName + filepath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath: cloudpath,
          filePath: filepath, // 文件路径
          success: res => {
            console.log(res)
            const db = wx.cloud.database()
            db.collection("emojis").add({
              data: {
                name: event.detail.value.eName,
                path: res.fileID
              } 
            })
          },
          fail: err => {
            console.log(err)
          }
        })
      }
    })
  },
  uploadVoice(event) {
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success (res) {
        const tempFilePath = res.tempFiles[0].path
        var cloudpath = "voice/" + event.detail.value.ch + tempFilePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath:cloudpath,
          filePath: tempFilePath,
          success: res => {
            const innerAudioContext = wx.createInnerAudioContext({
              useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
            })
            innerAudioContext.src = res.fileID
            innerAudioContext.play() // 播放
            const db = wx.cloud.database()
            db.collection("voice").add({
              data: {
                ch: event.detail.value.ch,
                en: event.detail.value.en,
                voice: event.detail.value.voice,
                path: res.fileID
              }
            })
          }
        })
      }
    })
  },
  choose1() {
    this.setData({
      c1: true,
      c2: false,
      bc1: "azure",
      bc2: "rgba(255,255,255,0.6)"
    })
  },
  choose2() {
    this.setData({
      c2: true,
      c1: false,
      bc2: "azure",
      bc1: "rgba(255,255,255,0)"
    })
  }
})